package com.lzy.shops.controller.backstage;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.lzy.shops.bean.Goods;
import com.lzy.shops.bean.PageBean;
import com.lzy.shops.service.GoodsService;
import com.lzy.shops.utils.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/system/goods")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    ObjectMapper objectMapper = new ObjectMapper();

    @GetMapping("/findAll")
    public String findAll(@RequestParam Map<String,Object> maps) throws JsonProcessingException {
        PageBean<Goods> allGoods = goodsService.findAll(maps);
        if(allGoods != null){
            System.out.println(allGoods);
            return objectMapper.writeValueAsString(new ResponseData<PageBean<Goods>>(true,allGoods,"findAll"));
        }
        return objectMapper.writeValueAsString(new ResponseData<PageBean<Goods>>(false,"服务器异常"));
    }

    @GetMapping("/findByGoodsId/{goodsId}")
    public String findByGoodsId(@PathVariable("goodsId") Integer goodsId){
        Collection<Goods> allGoods = goodsService.findByGoodsId(goodsId);
        if(allGoods != null){
            return allGoods.toString();
        }
        return "null";
    }

    @PostMapping("/addGoods")
    public String addGoods(@RequestBody Goods goods) throws JsonProcessingException {
        try{
            Integer integer = goodsService.addGoods(goods);
            if(integer!=null && integer!=0){
                return objectMapper.writeValueAsString(new ResponseData<Goods>(true,"添加成功"));
            }
        }catch (Exception e){
            return objectMapper.writeValueAsString(new ResponseData<Goods>(false,"添加失败"));
        }
        return objectMapper.writeValueAsString(new ResponseData<Goods>(false,"添加失败"));
    }

    @DeleteMapping("/delete/{shopsId}")
    public String deleteGoods(@PathVariable("shopsId") Integer shopsId) throws JsonProcessingException {
        Integer integer = null;
        try {
            integer = goodsService.deleteGoods(shopsId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(integer!=null && integer!=0){
            return objectMapper.writeValueAsString(new ResponseData<Goods>(true,"删除成功"));
        }
        return objectMapper.writeValueAsString(new ResponseData<Goods>(false,"操作失败"));
    }

    @PutMapping("/updateGoods")
    public String updateGoods(@RequestBody Goods goods) throws JsonProcessingException {
        Integer integer = null;
        try {
            integer = goodsService.updateGoods(goods);
        } catch (Exception e) {
            return objectMapper.writeValueAsString(new ResponseData<Goods>(false,"操作失败"));
        }
        return objectMapper.writeValueAsString(new ResponseData<Goods>(true,"修改成功"));
    }

    @GetMapping("/random")
    public String randomGoods(@RequestParam("type") String type) throws JsonProcessingException {
        ResponseData<Object> responseData;
        try {
            Goods goods = goodsService.getRandomGoods(type);
            responseData = new ResponseData<Object>(true,goods);
        } catch (Exception e) {
            responseData = new ResponseData<Object>(false,e.getMessage());
        }
        return objectMapper.writeValueAsString(responseData);
    }

    @GetMapping("/findGoodsBySearch")
    public String findGoodsBySearch(@RequestParam("search") String search) throws JsonProcessingException {
        ResponseData<Object> responseData;
        try {
            List<Goods> goodsBySearch = goodsService.findGoodsBySearch(search);
            responseData = new ResponseData<Object>(true,goodsBySearch);
        } catch (Exception e) {
            responseData = new ResponseData<Object>(false,new ArrayList<>());
        }
        return objectMapper.writeValueAsString(responseData);
    }
}
